import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
import matplotlib.pyplot as plt
import networkx as nx

# 1. 加载数据
df = pd.read_excel('C:\\Users\\Administrator\\Desktop\\关联规则\\餐厅数据.xlsx') 

# 2. 转换菜品格式：将逗号分隔的字符串拆分为列表
transactions = df['菜品'].str.split(',').to_list()

# 3. 数据标准化：转换为布尔矩阵（One-Hot编码）
te = TransactionEncoder()
te_ary = te.fit(transactions).transform(transactions)
df_encoded = pd.DataFrame(te_ary, columns=te.columns_)

# 4. 使用Apriori算法找出频繁项集
frequent_itemsets = apriori(df_encoded, min_support=0.1, use_colnames=True)
frequent_itemsets.sort_values(by='support', ascending=False, inplace=True)

# 5. 打印二项集
print(frequent_itemsets[frequent_itemsets.itemsets.apply(lambda x: len(x) == 2)])

# 6. 生成关联规则
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)  # 新增此行

# 7. 筛选有效规则（提升度>1且置信度>0.1）
effective = rules[
    (rules['lift'] > 1) & (rules['confidence'] > 0.1)
].sort_values(by=['lift', 'confidence'], ascending=False)

# 8. 打印筛选后的规则
print(effective[["antecedents", "consequents", "support", "confidence", "lift"]])